package com.returnsoft.softphone.eao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import com.returnsoft.softphone.entity.Campaign;
import com.returnsoft.softphone.entity.Server;
import com.returnsoft.softphone.exception.EaoExcepcion;

@LocalBean
@Stateless
public class CampaignEao implements Serializable{
	
	private static final long serialVersionUID = 1L;

	@PersistenceContext(name = "CCenterEJB", unitName = "CCenterEJB")
	private EntityManager em;

	public List<Campaign> findByServer(Integer serverId) throws EaoExcepcion {
		try {
			TypedQuery<Campaign> q = em
					.createQuery(
							"SELECT c FROM Campaign c left join c.server s where s.id = :serverId",
							Campaign.class);
			q.setParameter("serverId", serverId);
			List<Campaign> campaigns = q.getResultList();
			return campaigns;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public List<Campaign> findByAgent(Integer agentId) throws EaoExcepcion {

		try {
			Query q = em.createQuery("SELECT c.id,c.code,c.name,"
					+ "s.id,s.ip,s.amiUser,s.amiPassword,s.amiPort "
					+ "FROM Campaign c " + "left join c.agents a "
					+ "left join c.server s " + "where a.id = :agentId");
			q.setParameter("agentId", agentId);

			List<Object[]> objectCampaigns = (List<Object[]>) q.getResultList();
			List<Campaign> campaigns = new ArrayList<Campaign>();
			if (objectCampaigns.size() > 0) {
				for (Object[] objectCampaign : objectCampaigns) {
					Integer id = (Integer) objectCampaign[0];
					String code = (String) objectCampaign[1];
					String name = (String) objectCampaign[2];
					Integer serverId = (Integer) objectCampaign[3];
					String ip = (String) objectCampaign[4];
					String amiUser = (String) objectCampaign[5];
					String amiPassword = (String) objectCampaign[6];
					Integer amiPort = (Integer) objectCampaign[7];
					
					Campaign campaign = new Campaign();
					campaign.setId(id);
					campaign.setCode(code);
					campaign.setName(name);
					if (serverId != null && serverId > 0) {
						Server server = new Server();
						server.setId(serverId);
						server.setIp(ip);
						server.setAmiUser(amiUser);
						server.setAmiPassword(amiPassword);
						server.setAmiPort(amiPort);
						campaign.setServer(server);
					}
					campaigns.add(campaign);
				}
			}
			return campaigns;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Campaign> findBySupervisor(Integer supervisorId) throws EaoExcepcion {

		try {
			Query q = em.createQuery("SELECT c.id,c.code,c.name,"
					+ "s.id,s.ip,s.amiUser,s.amiPassword,s.amiPort "
					+ "FROM Campaign c " + "left join c.supervisors sp "
					+ "left join c.server s " + "where sp.id = :supervisorId");
			q.setParameter("supervisorId", supervisorId);

			List<Object[]> objectCampaigns = (List<Object[]>) q.getResultList();
			List<Campaign> campaigns = new ArrayList<Campaign>();
			if (objectCampaigns.size() > 0) {
				for (Object[] objectCampaign : objectCampaigns) {
					Integer id = (Integer) objectCampaign[0];
					String code = (String) objectCampaign[1];
					String name = (String) objectCampaign[2];
					Integer serverId = (Integer) objectCampaign[3];
					String ip = (String) objectCampaign[4];
					String amiUser = (String) objectCampaign[5];
					String amiPassword = (String) objectCampaign[6];
					Integer amiPort = (Integer) objectCampaign[7];
					
					Campaign campaign = new Campaign();
					campaign.setId(id);
					campaign.setCode(code);
					campaign.setName(name);
					if (serverId != null && serverId > 0) {
						Server server = new Server();
						server.setId(serverId);
						server.setIp(ip);
						server.setAmiUser(amiUser);
						server.setAmiPassword(amiPassword);
						server.setAmiPort(amiPort);
						campaign.setServer(server);
					}
					campaigns.add(campaign);
				}
			}
			return campaigns;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Campaign> findByCampaignId(Integer campaignId) throws EaoExcepcion {

		try {
			Query q = em.createQuery("SELECT c.id,c.code,c.name,"
					+ "s.id,s.ip,s.amiUser,s.amiPassword,s.amiPort "
					+ "FROM Campaign c " 
					+ "left join c.campaign cp "
					+ "left join c.server s " 
					+ "where cp.id = :campaignId");
			q.setParameter("campaignId", campaignId);

			List<Object[]> objectCampaigns = (List<Object[]>) q.getResultList();
			List<Campaign> campaigns = new ArrayList<Campaign>();
			if (objectCampaigns.size() > 0) {
				for (Object[] objectCampaign : objectCampaigns) {
					Integer id = (Integer) objectCampaign[0];
					String code = (String) objectCampaign[1];
					String name = (String) objectCampaign[2];
					Integer serverId = (Integer) objectCampaign[3];
					String ip = (String) objectCampaign[4];
					String amiUser = (String) objectCampaign[5];
					String amiPassword = (String) objectCampaign[6];
					Integer amiPort = (Integer) objectCampaign[7];
					
					Campaign campaign = new Campaign();
					campaign.setId(id);
					campaign.setCode(code);
					campaign.setName(name);
					if (serverId != null && serverId > 0) {
						Server server = new Server();
						server.setId(serverId);
						server.setIp(ip);
						server.setAmiUser(amiUser);
						server.setAmiPassword(amiPassword);
						server.setAmiPort(amiPort);
						campaign.setServer(server);
					}
					campaigns.add(campaign);
				}
			}
			return campaigns;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}

}
